package com.dsteiner.xelavos.client;


import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.RoundBorder;
import com.dsteiner.widgets.client.RoundBorder.RoundBorderModel;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.HorizontalPanelExt;
import com.dsteiner.widgets.client.Widgets.ImageExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.resources.RoundBorderCssResource;
import com.dsteiner.widgets.client.resources.RoundBorderResources;
import com.dsteiner.xelavos.client.controller.MainTabController;
import com.dsteiner.xelavos.client.controller.TabController;
import com.dsteiner.xelavos.client.model.MainTabModel;
import com.dsteiner.xelavos.client.model.SearchModel;
import com.dsteiner.xelavos.client.model.TabModel;
import com.dsteiner.xelavos.client.resources.XelavosImageBundle;
import com.dsteiner.xelavos.client.resources.XelavosResources;
import com.google.gwt.dom.client.Style.Cursor;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public class MainTabWidget extends LazyWidget{

	private class TabWidget extends LazyWidget{
		private final TabModel tabModel;
		private TabController tabController = new TabController();
		private final SearchModel searchModel;
		public TabWidget(TabModel tabModel, SearchModel searchModel) {
			this.tabModel = tabModel;
			this.searchModel = searchModel;
		}

		@Override
		public Widget createContentWidget() {
			HorizontalPanelExt mainPanel = new HorizontalPanelExt();
			ImageExt tabImage = new ImageExt(XelavosImageBundle.Util.getInstance().tabImage().createImage());
			ImageExt tabSelectedImage = new ImageExt(XelavosImageBundle.Util.getInstance().tabSelectedImage().createImage());
			HTMLExt link = new HTMLExt();
			RoundBorderCssResource css = RoundBorderResources.Util.INSTANCE.css();
			RoundBorderModel roundBorderModel = new RoundBorderModel();
			roundBorderModel.setContentWidget(mainPanel);
			roundBorderModel.setCssResource(css);
			RoundBorder decoratorPanel = new RoundBorder(roundBorderModel);
			ImageExt image = new ImageExt();
			
			mainPanel.add((Widget)image);
			mainPanel.add((Widget)link);

			mainPanel.setSize("100%", "100%");
			decoratorPanel.setWidth("100%");
			mainPanel.getElement().getStyle().setProperty("overflow", "visible");
			link.getElement().getStyle().setCursor(Cursor.POINTER);
			
			tabController.registerSearchModel(searchModel);
			tabController.registerImage(image);
			tabController.registerTabLink(link);
			tabController.registerTabModel(tabModel);
			tabController.registerMainTabModel(mainTabModel);
			tabController.registerTab(this);
			tabController.registerTabImage(tabImage);
			tabController.registerTabSelectedImage(tabSelectedImage);
			tabController.registerRoundBorderModel(roundBorderModel);
			tabController.registerJsUtil(JsUtil.getInstance());
			return decoratorPanel;
		}
	}

	private MainTabController mainTabController = new MainTabController();
	private final MainTabModel mainTabModel;
	private final SearchModel searchModel;

	public MainTabWidget(MainTabModel mainTabModel, SearchModel searchModel) {
		this.mainTabModel = mainTabModel;
		this.searchModel = searchModel;
	}
	
	@Override
	public Widget createContentWidget() {
		HorizontalPanel mainPanel = new HorizontalPanel();
		mainPanel.setSpacing(0);
		TabModel comprasTabModel = new TabModel();
		comprasTabModel.setText("compras");
		comprasTabModel.setImage(XelavosImageBundle.Util.getInstance().shoppingIcon());
		comprasTabModel.setTab(MainTabModel.TAB_ID_SHOPPING);
		TabWidget comprasTab = new TabWidget(comprasTabModel, searchModel);
		TabModel trabajoTabModel = new TabModel();
		trabajoTabModel.setText("trabajo");
		trabajoTabModel.setImage(XelavosImageBundle.Util.getInstance().jobIcon());
		trabajoTabModel.setTab(MainTabModel.TAB_ID_JOB);
		TabWidget trabajoTab = new TabWidget(trabajoTabModel, searchModel);
		TabModel entretenimientoTabModel = new TabModel();
		entretenimientoTabModel.setTab(MainTabModel.TAB_ID_LEISURE);
		entretenimientoTabModel.setText("recreacion");
		entretenimientoTabModel.setImage(XelavosImageBundle.Util.getInstance().leisureIcon());
		TabWidget entretenimientoTab = new TabWidget(entretenimientoTabModel, searchModel);
		TabModel amigosTabModel = new TabModel();
		amigosTabModel.setText("comunidad");
		amigosTabModel.setImage(XelavosImageBundle.Util.getInstance().communityIcon());
		amigosTabModel.setTab(MainTabModel.TAB_ID_COMMUNITY);
		TabWidget amigosTab = new TabWidget(amigosTabModel, searchModel);
		TabModel servicesTabModel = new TabModel();
		servicesTabModel.setText("servicios");
		servicesTabModel.setTab(MainTabModel.TAB_ID_SERVICES);
		servicesTabModel.setImage(XelavosImageBundle.Util.getInstance().serviceIcon());
		TabWidget servicesTab = new TabWidget(servicesTabModel, searchModel);
		
		mainPanel.add(comprasTab);
		mainPanel.add(servicesTab);
		mainPanel.add(trabajoTab);
		mainPanel.add(entretenimientoTab);
		mainPanel.add(amigosTab);
		
		setStyleName(XelavosResources.INSTANCE.css().mainTabWidget());
		
		mainTabController.registerMainTabModel(mainTabModel);
		mainTabController.registerComprasTabModel(comprasTabModel);
		mainTabController.registerServicesTabModel(servicesTabModel);
		mainTabController.registerTrabajoTabModel(trabajoTabModel);
		mainTabController.registerEntretenimientoTabModel(entretenimientoTabModel);
		mainTabController.registerAmigosTabModel(amigosTabModel);
		mainTabController.registerEventManager(EventManagerImpl.getInstance());
		return mainPanel;
	}

}
